package array;

/**
 * @author 陈正龙
 * @date 2022/3/3 9:35
 * 快排 排序数组
 */
public class _004 {
    public static void quickSort(int[] arr, int low, int high) {
        int leftPoint, rightPoint, temp, t;
        if (low > high) {
            return;
        }
        leftPoint = low;
        rightPoint = high;
        //temp就是基准位
        temp = arr[low];

        while (leftPoint < rightPoint) {
            //先看右边，依次往左递减
            while (temp <= arr[rightPoint] && leftPoint < rightPoint) {
                rightPoint--;
            }
            //再看左边，依次往右递增
            while (temp >= arr[leftPoint] && leftPoint < rightPoint) {
                leftPoint++;
            }
            //如果满足条件则交换
            if (leftPoint < rightPoint) {
                t = arr[rightPoint];
                arr[rightPoint] = arr[leftPoint];
                arr[leftPoint] = t;
            }

        }
        //最后将基准位与leftPoint和rightPoint相等位置的数字交换
        arr[low] = arr[leftPoint];
        arr[leftPoint] = temp;
        //递归调用左半数组
        quickSort(arr, low, rightPoint - 1);
        //递归调用右半数组
        quickSort(arr, rightPoint + 1, high);
    }


    public static void main(String[] args) {
        int[] arr = {10, 7, 2, 4, 7, 62, 3, 4, 2, 1, 8, 9, 19,15};
        quickSort(arr, 0, arr.length - 1);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }



}
